
package com.bk.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.bk.dao.impl.ProductDaoImpl;
import com.bk.model.Product;
import com.bk.service.ProductService;
import com.bk.utils.PageBean;

public class ProductServiceImpl implements ProductService{

	private ProductDaoImpl productDaoImpl=new ProductDaoImpl();
	@Override
	public PageBean findProductByPage(int currentPage, int pageSize,int cid) throws SQLException {
		PageBean pb=new PageBean();
		pb.setCurrentPage(currentPage);
		pb.setPageSize(pageSize);
		//totalCount 总个数
		String sql="select * from product where cid=?";
		List<Product> list = productDaoImpl.findByOthers(sql, cid);
		int totalCount = list.size();
		pb.setTotalCount(totalCount);
		//totalPageSize总页数
		int totalPageSize=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		pb.setTotalPageSize(totalPageSize);
		//每页的数据
		String sql_="select * from product where cid=? limit ?,?";
		List<Product> list_ = productDaoImpl.findByOthers(sql_, cid,(currentPage-1)*pageSize,pageSize);
		pb.setList(list_);	
		//TODO
		return pb;
			
	}
	@Override
	public PageBean findProductByPage(int currentPage, int pageSize, String param) throws SQLException {
		
		PageBean pb=new PageBean();
		pb.setCurrentPage(currentPage);
		pb.setPageSize(pageSize);
		//totalCount 总个数
		String sql="select * from product where pid like ? or pname like ? or pdesc like ? ";
	
		List<Product> list = productDaoImpl.findByOthers(sql,"%"+param+"%","%"+param+"%","%"+param+"%");
		System.out.println(sql);
		int totalCount = list.size();
		pb.setTotalCount(totalCount);
		//totalPageSize总页数
		int totalPageSize=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		pb.setTotalPageSize(totalPageSize);
		//每页的数据
		String sql_="select * from product where pid like ? or pname like ? or pdesc like ? limit ?,?";
		List<Product> list_ = productDaoImpl.findByOthers(sql_,"%"+param+"%","%"+param+"%","%"+param+"%",(currentPage-1)*pageSize,pageSize);
		pb.setList(list_);	
		//TODO
		return pb;
	}
	@Override
	public Product pruductDetail(String pid) throws SQLException {
		
		//TODO
		 List<Product> list = productDaoImpl.findByOthers("select * from product where pid=?", pid);
		 
		if(list.size()>0){
			return list.get(0);
					
		}	
		return null;
	}

}

	